package com.icongyou.enterprise.talent_analysis.mapper;

import com.icongyou.enterprise.talent_analysis.entity.AIInsightCache;
import org.apache.ibatis.annotations.*;

/**
 * AI洞察缓存Mapper
 */
@Mapper
public interface AIInsightCacheMapper {
    
    @Select("SELECT * FROM ai_insight_cache WHERE student_id = #{studentId} " +
            "AND (template_id = #{templateId} OR (template_id IS NULL AND #{templateId} IS NULL)) " +
            "AND expire_at > NOW()")
    AIInsightCache selectByStudentAndTemplate(@Param("studentId") String studentId, 
                                              @Param("templateId") String templateId);
    
    @Insert("INSERT INTO ai_insight_cache (id, student_id, template_id, insight_json, " +
            "ai_model_version, generated_at, updated_at, expire_at, version) " +
            "VALUES (#{id}, #{studentId}, #{templateId}, #{insightJson}, " +
            "#{aiModelVersion}, #{generatedAt}, #{updatedAt}, #{expireAt}, #{version})")
    int insert(AIInsightCache cache);
    
    @Update("UPDATE ai_insight_cache SET insight_json = #{insightJson}, " +
            "updated_at = #{updatedAt}, expire_at = #{expireAt}, version = version + 1 " +
            "WHERE id = #{id} AND version = #{version}")
    int updateByIdWithVersion(AIInsightCache cache);
    
    @Delete("DELETE FROM ai_insight_cache WHERE student_id = #{studentId}")
    int deleteByStudentId(@Param("studentId") String studentId);
    
    @Delete("DELETE FROM ai_insight_cache WHERE template_id = #{templateId}")
    int deleteByTemplateId(@Param("templateId") String templateId);
    
    @Delete("DELETE FROM ai_insight_cache WHERE expire_at < NOW()")
    int deleteExpired();
}
